package com.haiyou.common.util;


import com.haiyou.common.exception.BusinessException;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author chenzeke
 */
public class StringUtils {

    /**
     * 表示对象为空或长度为0的String、空格字符串(空白的)
     *
     * @param str 字符串
     * @return 返回true/false
     */
    public static boolean isBlank(String str) {
        return (str == null || "".equals(str.trim()));
    }

    public static boolean isNotBlank(String str) {
        return !isBlank(str);
    }

    /**
     * 表示对象为空或长度为0的String(空)
     *
     * @param str 字符串
     * @return 返回true/false
     */
    public static boolean isEmpty(String str) {
        return (str == null || "".equals(str));
    }

    public static boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }

    /**
     * 获取u1u2
     *
     * @param uone 用户1
     * @param utwo 用户2
     * @return 返回用户1和用户2的拼接字符串
     */
    public static String compare(String uone, String utwo) {
        return uone.compareToIgnoreCase(utwo) < 0 ? uone + "_" + utwo : utwo + "_" + uone;
    }

    /**
     * 检验QQ号码格式是否正确
     *
     * @param str 输入QQ号码
     */
    public static void checkQQ(String str) {

        if (StringUtils.isNotBlank(str)) {
            String regEx = "^[1-9][0-9]{4,9}$";
            Pattern pattern = Pattern.compile(regEx);
            Matcher matcher = pattern.matcher(str);
            if (!matcher.find()) {
                throw new BusinessException("Q格式错误！并且最长只能10位！");
            }
        }
    }

    /**
     * 检验电话号码格式是否正确
     *
     * @param str 输入座机号码
     */
    public static void checkPhone(String str) {
        if (StringUtils.isNotBlank(str)) {
            String regEx = "^[1-9]{1}[0-9]{5,8}$";
            Pattern pattern = Pattern.compile(regEx);
            Matcher matcher = pattern.matcher(str);
            if (!matcher.find()) {
                throw new BusinessException("电话号码格式错误！");
            }
        }
    }

    /**
     * 检验密码安全强度
     */
    public static void checkPassWord(String str) {
        if (StringUtils.isNotBlank(str)) {
            String regEx = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,20}$";
            Pattern pattern = Pattern.compile(regEx);
            Matcher matcher = pattern.matcher(str);
            if (!matcher.find()) {
                throw new BusinessException("密码8-20位，必须包含数字和字母");
            }
        }
    }

    /**
     * 判断number参数是否是浮点数表示方式
     *
     * @param number 数值字符串
     * @return 返回结果
     */
    public static boolean isFloat(String number) {
        number = number.trim();
        //浮点数的正则表达式-小数点在中间与前面
        String pointPrefix = "(\\-|\\+){0,1}\\d*\\.\\d+";
        //浮点数的正则表达式-小数点在后面
        String pointSuffix = "(\\-|\\+){0,1}\\d+\\.";
        return number.matches(pointPrefix) || number.matches(pointSuffix);
    }

    /**
     * 判断number参数是否是整型数表示方式
     *
     * @param number 数值字符串
     * @return 返回结果
     */
    public static boolean isInteger(String number) {
        number = number.trim();
        //整数的正则表达式
        String intNumRegex = "\\-{0,1}\\d+";
        return number.matches(intNumRegex);
    }

    public static String getUuId() {
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        return uuid;
    }

    public static List<Integer> strToNumList(String str) {
        List<Integer> list = new ArrayList<>();
        String[] strs = str.split(",");
        if (strs.length == 0) {
            return null;
        }
        for (int i = 0; i < strs.length; i++) {
            list.add(Integer.valueOf(strs[i]));
        }
        return list;
    }

    public static List<String> strToList(String str) {
        List<String> list = new ArrayList<>();
        String[] strs = str.split(",");
        if (strs.length == 0) {
            return null;
        }
        for (int i = 0; i < strs.length; i++) {
            list.add(strs[i]);
        }
        return list;
    }

    public static String listToStr(List<String> list) {
        StringBuilder sb = new StringBuilder();
        if (list != null || list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                if (i == 0) {
                    sb.append(list.get(i));
                } else {
                    sb.append(",").append(list.get(i));
                }
            }
        }
        return sb.toString();
    }


}
